<script>
	import user from '@/api/user.js';
	import getLocation from './utils/location.js';
	export default {
		data() {
			return {
				shareId: '',
				InvitationCode: ''
			}
		},
		onLaunch() {
			// uni.setStorageSync('shareId', '')
			// uni.setStorageSync('InvitationCode', '')
			getLocation().then(res => {}).catch(err => {})
		},
		onShow({
			query
		}) {
			if(!this.$store.state.isShare) return
			console.log(query, '分享参数')
			uni.setStorageSync('shareId', '')
			uni.setStorageSync('InvitationCode', '')
			
			if (query.userid) { //分享人id
				// if(this.shareId == query.userid) return
				if (uni.getStorageSync('userInfo').id == query.userid) return
				if (uni.getStorageSync('token')) {
					this.memberShare(query)
				} else {
					uni.setStorageSync('shareId', query.userid)
				}
			}
			if (query.scene) { ///邀请码
				// if(this.InvitationCode == query.scene) return
				query.scene = decodeURIComponent(query.scene).split('invitation_code=')[1]
				console.log(query.scene, '邀请码')
				if (uni.getStorageSync('token')) {
					this.editMemberInfo(query.scene)
				} else {
					uni.setStorageSync('InvitationCode', query.scene)
				}
				
			}
		},
		methods: {
			editMemberInfo(invitation_code) {
				user.editMemberInfo({
					id: uni.getStorageSync('userInfo').id,
					third_invitation_code: invitation_code

				}).then((res) => {
					if (res.code === 200) {
						uni.setStorageSync('InvitationCode', '')
					} else {
						uni.setStorageSync('InvitationCode', invitation_code)
					}
					this.InvitationCode = invitation_code
				}).catch(() => {
					uni.setStorageSync('InvitationCode', invitation_code)
					this.InvitationCode = invitation_code
				})

			},
			async memberShare(query) {
				const res = await user.memberShare({
					id: query.userid
				})
				if (res.code === 200) {
					if(this.shareId != query.userid){
						uni.showToast({
							title: '绑定成功',
							icon: 'none'
						})
					}
					uni.setStorageSync('shareId', '')
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					uni.setStorageSync('shareId', obj.id)
				}
				this.shareId = query.userid
			},
			getMessgeAuth() {
				let _this = this
				wx.getSetting({
					withSubscriptions: true,
					success: res => {
						// 判断是否已经授权
						if (res.authSetting['scope.subscribeMessage']) {
							console.log('用户点击了“总是保持以上，不再询问”')
						} else {
							_this.setAuth(res)
						}
					}
				})
			},
			//获取消息订阅权限
			setAuth(res) {
				if (res.subscriptionsSetting.mainSwitch) {
					wx.showModal({
						title: '提示',
						content: '授权开通订阅消息通知',
						showCancel: true,
						success: function(ress) {
							if (ress.confirm) {
								wx.requestSubscribeMessage({ // 调起消息订阅界面
									tmplIds: [
										'OV2gqOd2Jl6GccNG_NSGH2nbLOP_tgbMPBEr9LiMhI8',
										'0No1dH3C5vVnKjVHlsJRBoBTlASJfhBe86A0TSfmmKk',
										'qjsUDq0CjYKNgO1QIvOCoii09ARpNhxbwfuzlaYcrYc'
									],
									success(res) {
										console.log('订阅消息 成功 ');
									},
									fail(er) {
										console.log("订阅消息 失败 ");
									}
								})
							}
						}
					})
				} else {
					wx.showModal({
						content: '检测到您没打开消息推送权限，是否去设置打开？',
						success: function(res) {
							if (res.confirm) {
								wx.openSetting()
							}
						},
					});
				}
			}
		},
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "./uni_modules/vk-uview-ui/index.scss";
	@import "./static/css/style.scss";

	@import url("/static/icons/iconfont.css");
	@import url("/static/icons/iconfont-weapp.css");

	/* 去除默认滚动条效果 */
	::-webkit-scrollbar {
		display: none;
		width: 0;
		height: 0;
		color: transparent;
	}
</style>